import { ref } from "vue";
import { NetworkInviteCodeSet } from "../types";
import { generateInviteCode, getNetworkInviteCodeSet, saveNetworkInviteCode } from "../../../../../../../../api/basic";

// 获取设置加载
const getNetworkInviteCodeSetLoading = ref(false)

// 服务商邀请码设置
const networkInviteCodeSet = ref<NetworkInviteCodeSet | null>(null)

// 获取服务商邀请码设置方法
const getNetworkInviteCodeSetFn = async () => {
    getNetworkInviteCodeSetLoading.value = true
    const res = await getNetworkInviteCodeSet()
    networkInviteCodeSet.value = res.data.data.row
    getNetworkInviteCodeSetLoading.value = false
}

// 生成/重新生成邀请码加载
const generateInviteCodeLoading = ref(false)

// 生成/重新生成邀请码方法
const generateInviteCodeFn = async () => {
    generateInviteCodeLoading.value = true
    const res = await generateInviteCode()
    if (networkInviteCodeSet.value) {
        networkInviteCodeSet.value.inviteCode = res.data.data.row.inviteCode
    }
    generateInviteCodeLoading.value = false
}

// 保存服务商邀请码设置加载
const saveNetworkInviteCodeLoading = ref(false)

// 保存服务商设置方法
const saveNetworkInviteCodeFn = async () => {
    saveNetworkInviteCodeLoading.value = true
    if (networkInviteCodeSet.value) {
        const res = await saveNetworkInviteCode(networkInviteCodeSet.value)
        saveNetworkInviteCodeLoading.value = false
        return res.data
    }
}

export default {
    networkInviteCodeSet,
    generateInviteCodeLoading,
    saveNetworkInviteCodeLoading,
    getNetworkInviteCodeSetLoading,
    generateInviteCodeFn,
    saveNetworkInviteCodeFn,
    getNetworkInviteCodeSetFn,
}
